<?php
/**
 * @todo: Show error in validation and restoring the validated fields 
 */
class edit extends a_admin_controller {
    protected $srv;
    protected $tracker;

    public function __construct() {
        parent::__construct(true);
        $this->srv = new service_l_analytics;
    }

    public function batch_assign(){
       $this->smarty()->assign("tracker", $this->tracker);
    }

    public function index(){
    }

    public function views($tracker_id){
        $this->tracker = $this->srv->get_tracker($tracker_id);
    }

    public function delete_tracker($tracker_id)
    {
        if($this->srv->delete_tracker($tracker_id))
            redirect(base_url()."index.php/analytics/admin/analytics/");
        else
            trace("ERROR: Tracker was not deleted!");
    }

    public function do_edit()
    {
        $tracker_id = $this->input->post('tracker_id');

        $ga_tracking_id = trim($this->input->post('ga_tracking_id'));
        $site_id = trim($this->input->post('site_id'));
        $page_tracking = trim($this->input->post('page_tracking'));
        $ecommerce_tracking = trim($this->input->post('ecommerce_tracking'));
        $event_tracking = trim($this->input->post('event_tracking'));

        if($ga_tracking_id && is_numeric($site_id))
        {
            if( strlen($ga_tracking_id)  && strlen($site_id))
            {
                if($this->srv->save_tracker(
                        array(
                            'tracker_id'         => $tracker_id,
                            'ga_tracking_id'     => $ga_tracking_id,
                            'site_id'            => $site_id,

                            'page_tracking'      => strcmp($page_tracking, "on") ? 0 : 1 ,
                            'ecommerce_tracking' => strcmp($ecommerce_tracking, "on") ? 0 : 1 ,
                            'event_tracking'     => strcmp($event_tracking, "on") ? 0 : 1 
                        )
                        )
                  ){
                    redirect("analytics/admin/trackers/");
                }
                else{
                    $this->add_global_msg("Tracker was not saved");
                }
            }
        }
        else
        {
            $this->add_global_msg("Some of the fields are empty");
        }
    }

}

?>